<?php

namespace app\api\controller\v1\home;

use app\api\model\BannerModel;
use app\api\model\CateGoryModel;
use app\api\model\GoodsModel;
use app\api\model\UserModel;
use app\middleware\ApiAuth;
use think\db\exception\DataNotFoundException;
use think\facade\Db;
use think\Request;
use think\facade\Validate;
use think\response\Json;
use think\validate\ValidateRule;
use app\common\Image;
/**
 * Class C端首页控制器
 * @package app\controller
 */
class Index
{
    protected $middleware = [ApiAuth::class];

    /**
     * @desc 获取首页数据
     * @return Json
     */
    public function index(Request $request)
    {
        // 获取Access_token
        $input['admin_id'] = $request->param('admin_id');//运营商ID
        $input['center_ids'] = $request->param('center_ids');//分拣中心IDs
        // 参数校验
        $validate = Validate::rule([
            'admin_id' => ValidateRule::isRequire(),
//            'center_ids'=> ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        $banner = BannerModel::GetBanner($input);
        $categroy = CateGoryModel::GetCategory($input);
        $is_new = GoodsModel::GetGoodsNew($input);
        $is_hot = GoodsModel::GetGoodsHot($input);
        $is_activity = GoodsModel::GetGoodsActivity($input);
        return jsonSuccess('获取成功', compact('banner', 'categroy', 'is_new', 'is_hot','is_activity'));
    }

    /**
     * @desc 商品搜索
     * @return Json
     */
    public function search(Request $request)
    {
        // 获取Access_token
        // $input['cate_id'] = $request->param('cate_id');//三级分类
        $input['text'] = $request->param('text');//商品名称
        $input['center_ids'] = $request->param('center_ids');//分拣中心IDs
        $input['page'] = $request->param('page');//页
        $input['priceOrder'] = $request->param('priceOrder');//价格排序
        $input['salesOrder'] = $request->param('salesOrder');//销量排序
        $input['is_new'] = $request->param('is_new');//是否新品
        $input['is_hot'] = $request->param('is_hot');//是否热卖
        $input['is_activity'] = $request->param('is_activity');//是否活动中
        // 参数校验
        $validate = Validate::rule([
            'text' => ValidateRule::isRequire(null, '请输入商品名称'),
            'center_ids' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isInteger(null, '缺少参数')->gt(0,'不合法参数'),
            'priceOrder' => function ($v,$all) {
                if (!in_array($v, ['', 'asc', 'desc'], true)) {
                    return '不合法参数';
                }
                if ($v && $all['salesOrder']) {
                    return '一次只能进行一种排序';
                }
                return true;
            },
            'salesOrder' => function ($v, $all) {
                if (!in_array($v, ['', 'asc', 'desc'], true)) {
                    return '不合法参数';
                }
                if ($v && $all['priceOrder']) {
                    return '一次只能进行一种排序';
                }
                return true;
            },
            'is_new' => function ($v) {
                return in_array($v, ['', '1'], true) ? true : '不合法参数' ;
            },
            'is_hot' => function ($v) {
                return in_array($v, ['', '1'], true) ? true : '不合法参数' ;
            },
            'is_activity' => function ($v) {
                return in_array($v, ['', '1'], true) ? true : '不合法参数' ;
            },
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return GoodsModel::search($input);
    }

    /**
     * @desc 商品详情
     * @return Json
     */
    public function detail(Request $request)
    {
        $input['admin_id'] = $request->param('admin_id');//运营商ID
        $input['goods_id'] = $request->param('goods_id');//商品ID
        $input['group_id'] = $request->param('group_id');//公司id
        // 参数校验
        $validate = Validate::rule([
            'goods_id' => ValidateRule::isRequire(),
            'admin_id' => ValidateRule::isRequire(),
            'group_id' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return GoodsModel::detail($input);
    }
    /**
     * @desc 员工列表
     * @return Json
     */
    public function EmployeeList(Request $request)
    {
        $input['uid'] = $request->param('uid');//老板ID
        $input['group_id'] = $request->param('group_id');//公司ID
        // 参数校验
        $validate = Validate::rule([
            'uid' => ValidateRule::isRequire(),
            'group_id' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return UserModel::EmployeeList($input);
    }

    /**
     * @desc 老板审核员工
     * @return Json
     */
    public function IsAdopt(Request $request)
    {
        $input['id'] = $request->param('id');//员工ID
        $input['uid'] = $request->param('uid');//老板ID
        $input['is_adopt'] = $request->param('is_adopt');//'1:通过 2：未通过',
        // 参数校验
        $validate = Validate::rule([
            'is_adopt' =>  function($value,$all) {
               if(in_array($value,[1,2])){
                   return true;
               }else{
                   return '参数错误';
               }
            },
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return UserModel::IsAdopt($input);
    }
    /**
     * @desc 老板解除员工
     * @return Json
     */
    public function RelieveEmployee(Request $request)
    {
        $input['id'] = $request->param('id');//员工ID
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return UserModel::RelieveEmployee($input);
    }
    /**
     * @desc 上传图片
     * @return Json
     */
    public function UploadImage(Request $request)
    {
        // 获取参数
        $input['image'] = $request->file('image');
        // 参数校验
        $validate = Validate::rule([
            'image' => ValidateRule::isRequire(null, '缺少参数')
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        $pic = Image::uploadSingle($input['image'], 'user');
        if (!is_string($pic)) return $pic;
        return jsonSuccess('添加成功',$request->scheme() . '://' . $request->host() . '/api/uploads/' . str_replace('\\', '/', $pic));
    }

    /**
     * @desc 获取系统配置
     * @return Json
     */
    public function GetConfig(Request $request)
    {
        // 获取参数
        $input['admin_id'] = $request->param('admin_id');
        // 参数校验
        $validate = Validate::rule([
            'admin_id' => ValidateRule::isRequire(null, '缺少参数')
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        $data['customer_phone'] = Db::table('eb_config')->where('admin_id',$input['admin_id'])->where('name','customer_phone')->find();
        $data['automatic_refuse'] = Db::table('eb_config')->where('admin_id',$input['admin_id'])->where('name','automatic_refuse')->find();
        return jsonSuccess('获取成功',$data);
    }
}
